
function delCartItem(yourCartId) {
	var http = new XMLHttpRequest();
	var s = "yourCartId=" + yourCartId + "&action=deleteItem";
	console.log(s);

	http.open("POST", "updateCart");

	http.onreadystatechange = function() {

		if (http.readyState == 4) {
			var cartId = "your_cart_item_" + yourCartId;
			$("#" + cartId).hide();

		}
	};

	http.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded; charset=ISO-8859-1;");

	http.send(s);
}
function delFavouriteItem(favouriteId) {
	var http = new XMLHttpRequest();
	var s = "favouriteId=" + favouriteId + "&action=deleteItem";
	console.log(s);

	http.open("POST", "favourite");

	http.onreadystatechange = function() {

		if (http.readyState == 4) {
			var totalMoney = document.getElementById("totalMoney");
			var cartId = "your_favourite_item_" + favouriteId;
			$("#" + cartId).hide();
			totalMoney.innerHTML = http.responseText;

			$("#status_money").show();

		}
	};

	http.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded; charset=ISO-8859-1;");

	http.send(s);
}

function updateNumber(yourCartId) {
	var http = new XMLHttpRequest();
	var number = document.getElementById("number_" + yourCartId).value;
	var s = "yourCartId=" + yourCartId + "&number=" + number
			+ "&action=updateNumber";

	console.log(s);

	http.open("POST", "updateCart");

	http.onreadystatechange = function() {

		if (http.readyState == 4) {
			var totalMoney = document.getElementById("totalMoney");

			totalMoney.innerHTML = http.responseText;

			$("#status_money").show();

		}
	};

	http.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded; charset=ISO-8859-1;");

	http.send(s);
}

function getMoney(price, yourCartId) {
	var number = document.getElementById("number_" + yourCartId).value;
	var money = number * price;
	document.getElementById("money_" + yourCartId).innerHTML = money;
	$("#status_money").hide();

}

function addFavourite(productId) {
	var p = "productId=" + productId;
	var http = new XMLHttpRequest();
	var divItem = document.getElementById("add_favourite");
	var success = document.createElement("a");
	success.attributes = "#";
	http.open("POST", "favourite?productId=" + productId);
	http.send(p);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			var resutl = http.responseText;

			divItem.innerHTML = resutl;

		}
	};

}

function favouriteToCart(productId)

{
	var http = new XMLHttpRequest();
	var s = "productId=" + productId + "&action=addToCart";
	console.log(s);

	http.open("POST", "favourite");

	http.onreadystatechange = function() {

		var divItem = document.getElementById("favouriteToCart_" + productId);
		divItem.innerHTML = "Da Them Vao Gio Hang";

	};

	http.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded; charset=ISO-8859-1;");

	http.send(s);

}
function addCart(productId) {
	var p = "productId=" + productId;
	var http = new XMLHttpRequest();
	var divItem = document.getElementById("add_cart");
	var success = document.createElement("a");
	success.attributes = "#";
	http.open("POST", "addCart?productId=" + productId);
	http.send(p);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			var resutl = http.responseText;

			divItem.innerHTML = resutl;

		}
	};

}

function getPrice(type, tongtien) {

	var t = document.getElementById(type).value;
	var display_price = document.getElementById("price_" + type);

	// alert(tongtien.value);

	var http = new XMLHttpRequest();
	if (t != "luachon") {
		http.open("GET", "selectProfile?productId=" + t, true);
		http.send();
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				display_price.innerHTML = http.responseText;
			}
		};
	} else {
		display_price.innerHTML = "0";

	}

	$("#status_total_money_profile").hide();

	// document.getElementById("total_money").innerHTML = tongtien;

	// alert("price_"+type);
	// display_price.innerHTML = t;
}

function getTotalMoneyProfile() {

	var tongtien = 0;

	tongtien = tongtien
			+ parseInt(document.getElementById("price_cpu").innerHTML, 10);
	tongtien = tongtien
			+ parseInt(document.getElementById("price_mainboard").innerHTML, 10);
	tongtien = tongtien
			+ parseInt(document.getElementById("price_ram").innerHTML, 10);
	tongtien = tongtien
			+ parseInt(document.getElementById("price_hdd").innerHTML, 10);
	tongtien = tongtien
			+ parseInt(document.getElementById("price_lcd").innerHTML, 10);
	tongtien = tongtien
			+ parseInt(document.getElementById("price_mouse").innerHTML, 10);
	tongtien = tongtien
			+ parseInt(document.getElementById("price_keyboard").innerHTML, 10);
	document.getElementById("total_money").innerHTML = tongtien;
	$("#status_total_money_profile").show();
	// tongtien = tongtien +
	// parseInt(document.getElementById("price_"+type).innerHTML, 10);
}

function paymentSuccess() {
	alert("Đã Thanh Toán Thành Công !!!!!");

}

/* nap tien vao tai khoan */
function addCard() {
	var http = new XMLHttpRequest();
	var s = "cardId=" + document.getElementsByName("serialCard")[0].value;
	// alert(s);
	if (document.getElementsByName("serialCard")[0].value == "") {
		alert("Bạn chưa nhập mã số thẻ cào !!!");
	} else {

		console.log(s);

		http.open("POST", "card");

		http.onreadystatechange = function() {

			if (http.readyState == 4) {
				var display = document.getElementById("displayMoney");

				var resutl = http.responseText;
				if (resutl == "false") {
					alert("Mã số thẻ không hợp lệ");
				} else {
					display.innerHTML = resutl;
					alert("Nạp tiền thành công");
				}

				document.getElementsByName("serialCard")[0].value = "";

			}
		};

		http.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded; charset=ISO-8859-1;");

		http.send(s);
	}
}

$(document)
		.ready(
				function() {
					$("#change_pass_form").hide();
					$("#link_change_pass").click(function() {
						$("#change_pass_form").slideToggle("slow");
					});

					$("#change_pass_cancel").click(function() {
						$("#change_pass_form").hide("slow");
					});

					$("#change_pass_submit")
							.click(
									function() {
										var oldPass = $("#oldPass").val();
										var newPass = $("#newPass").val();
										var newPassAgain = $("#newPassAgain")
												.val();
										// alert(oldPass + newPass +
										// newPassAgain);
										// ajax day du lieu len server

										if (oldPass == "" || newPass == ""
												|| newPassAgain == "") {
											alert("Hãy nhập đầy đủ thông tin !!!");

										} else {
											var http = new XMLHttpRequest();
											var s = "oldPass=" + oldPass
													+ "&newPass=" + newPass
													+ "&newPassAgain="
													+ newPassAgain;
											console.log(s);

											http.open("POST", "changePass");

											http.onreadystatechange = function() {

												if (http.readyState == 4) {
													// alert(http.responseText);
													if (http.responseText == "true") {

														// alert("Đổi mật khẩu
														// thành công !!!");
														$("#link_change_pass")
																.hide();
														$("#change_pass_form")
																.html(
																		"Đổi mật khẩu thành công !!!");
													} else
														alert("bạn nhập sai mật khẩu hoặc mật khẩu mới không trùng nhau !!!");

												}
											};

											http
													.setRequestHeader(
															"Content-type",
															"application/x-www-form-urlencoded; charset=ISO-8859-1;");

											http.send(s);
										}

									});
					startTime();
				});

function startTime() {
	var today = new Date();
	var hour = today.getHours();
	var minute = today.getMinutes();
	var second = today.getSeconds();

	var year = today.getFullYear();
	var month = today.getMonth();
	var day = today.getDate();

	minute = checkTime(minute);
	second = checkTime(second);

	document.getElementById('date').innerHTML = "Ngày " + day + " Tháng "
			+ month + " năm " + year + "&nbsp&nbsp&nbsp&nbsp" + hour + " : "
			+ minute + " : " + second;

	setTimeout('startTime()', 1000);
}

function checkTime(i) {
	if (i < 10) {
		i = "0" + i;
	}
	return i;
}

function getProductWithProducerId(categoryName, categoryId) {
	var http = new XMLHttpRequest();
	var e = document.getElementsByName('producer ' + categoryName)[0];
	var producerId = e.options[e.selectedIndex].value;
	
	var s = "mode=getproduct" + "&categoryId=" + categoryId + "&producerId=" + producerId;
	http.open("POST", "getProduct");

	http.onreadystatechange = function() {

		if (http.readyState == 4) {
			var e1 = document.getElementsByName(categoryName)[0];
			e1.innerHTML = http.responseText;
			e1.disabled = false;
		}
	};

	http.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded; charset=ISO-8859-1;");

	http.send(s);
}

function getPriceProduct(categoryName) {
	var e = document.getElementsByName(categoryName)[0];
	var productId = e.options[e.selectedIndex].value;
	
	if(productId == "-1") {
		var e1 = document.getElementById(categoryName);
		var e2 = document.getElementById("sumprice");
		
		e2.innerHTML = parseInt(e2.innerHTML, 10) - parseInt(e1.innerHTML, 10);
		e1.innerHTML = "";
		
		return;
	}
	
	var http = new XMLHttpRequest();
	var s = "mode=getprice" + "&productId=" + productId;
	http.open("POST", "getProduct");

	http.onreadystatechange = function() {

		if (http.readyState == 4) {
			var e1 = document.getElementById(categoryName);
			var e2 = document.getElementById("sumprice");
			
			if(e1.innerHTML != "")
				e2.innerHTML = parseInt(e2.innerHTML, 10) - parseInt(e1.innerHTML, 10);
			e1.innerHTML = http.responseText;
			e2.innerHTML =  parseInt(e2.innerHTML, 10) + parseInt(e1.innerHTML, 10);
		}
	};

	http.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded; charset=ISO-8859-1;");
	http.send(s);
}

function searchLostFocus() {
	var e = document.getElementsByName("text-search")[0].value;
	
	if(e == '')
		document.getElementsByName('text-search')[0].value = 'Nhập để tìm kiếm';
}

function searchGetFocus() {
	if(document.getElementsByName("text-search")[0].value == 'Nhập để tìm kiếm')
		document.getElementsByName("text-search")[0].value = '';
}